package com.op.studyingliteapp.fragment.seminar;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.op.studyingliteapp.R;

/**
 * by kim 2015.7.25
 */
public class SeminarAdapter extends BaseAdapter {
	private static final String TAG = SeminarAdapter.class.getSimpleName();
	private Context mContext;
	private ArrayList<SeminarData> discussion_List;
	private DisplayImageOptions options;

	public SeminarAdapter(Context mContext,
			ArrayList<SeminarData> discussion_List) {
		this.mContext = mContext;
		this.discussion_List = discussion_List;
	}

	public Context getmContext() {
		return mContext;
	}

	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		// 列表的数量
		return discussion_List.size();
	}

	@Override
	public SeminarData getItem(int position) {
		// TODO Auto-generated method stub
		return discussion_List.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public String getSeminarId(int position) {
		return discussion_List.get(position).getSeminarId();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		LinearLayout linearLayout = null;
		if (convertView != null) {
			linearLayout = (LinearLayout) convertView;
		} else {
			linearLayout = (LinearLayout) LayoutInflater.from(getmContext())
					.inflate(R.layout.item_seminar, parent, false);
		}

		final SeminarData data = getItem(position);

		ImageView avatar = (ImageView) linearLayout.findViewById(R.id.Avatar);// 头像
		TextView name = (TextView) linearLayout.findViewById(R.id.name);// 名字
		TextView title = (TextView) linearLayout.findViewById(R.id.title);// 标题
		TextView context = (TextView) linearLayout.findViewById(R.id.context);
		TextView time = (TextView) linearLayout.findViewById(R.id.time);
		ImageLoader.getInstance().displayImage(data.getHeadPortrait(), avatar,
				options);

		name.setText(data.getName());
		title.setText(data.getTitle());
		context.setText(data.getWord());
		time.setText(data.getTime());

		return linearLayout;
	}

}
